<script setup lang="ts">
import useLoginStore from '@/store/login/login'
import type { IUserMenu } from '@/service/login/type'

const props = defineProps<{
  collapse: boolean
}>()

const { collapse } = toRefs(props)

const loginStore = useLoginStore()
const { userMenus } = storeToRefs(loginStore)

const convertIconStr = (icon: string): string => {
  const iconArr = icon.split('-')
  if (iconArr.length > 2) {
    iconArr.shift()
    iconArr.shift()
  }
  return iconArr.join('-')
}

const router = useRouter()
const handleMenuItemClick = (menuItem: IUserMenu) => {
  router.push(menuItem.url ?? '/not-found')
}
</script>

<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="img" src="~@/assets/img/logo.svg" alt="logo" />
      <span v-if="!collapse" class="title">Vue3+TS</span>
    </div>
    <el-menu
      :collapse="collapse"
      default-active="2"
      background-color="#0c2135"
      text-color="#b7bdc3"
      active-text-color="#0a60bd"
      class="el-menu-vertical"
    >
      <template v-for="item in userMenus" :key="item.id">
        <!-- 二级菜单 -->
        <template v-if="item.type === 1">
          <!-- 二级菜单可以展开的标题 -->
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <el-icon v-if="item.icon">
                <component :is="convertIconStr(item.icon)"></component>
              </el-icon>
              <span>{{ item.name }}</span>
            </template>
            <!-- 遍历里面的item -->
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item
                :index="subitem.id + ''"
                @click="handleMenuItemClick(subitem)"
              >
                <template #title>
                  <el-icon v-if="subitem.icon">
                    <component :is="convertIconStr(subitem.icon)"></component>
                  </el-icon>
                </template>

                <span>{{ subitem.name }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <!-- 一级菜单 -->
        <template v-else-if="item.type === 2">
          <el-menu-item
            :index="item.id + ''"
            @click="handleMenuItemClick(item)"
          >
            <template #title>
              <el-icon v-if="item.icon">
                <component :is="convertIconStr(item.icon)"></component>
              </el-icon>
            </template>

            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<style lang="scss" scoped>
.nav-menu {
  height: 100%;
  background-color: #001529;

  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;

    .img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
  }

  :deep(.el-menu) {
    border-right: none;
  }

  // 目录
  :deep(.el-menu .el-sub-menu) {
    background-color: #001529 !important;
    // 二级菜单 ( 默认背景 )
  }

  :deep(.el-menu-item) {
    padding-left: 50px !important;
    background-color: #0c2135 !important;
    &:hover {
      color: #fff !important; // 菜单
    }
    &.is-active {
      color: #fff !important;
      background-color: #0a60bd !important;
    }
  }

  :deep(.el-sub-menu__title) {
    background-color: #001529 !important;
  }
}

:deep(.el-menu-item-group__title) {
  padding: 0;
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  height: calc(100% - 48px);
}
</style>
